package com.turbo.cloud.contract.web.logic;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.turbo.cloud.contract.web.entity.CtContractTemplateOverlay;
import com.turbo.cloud.contract.web.model.overlay.CtContractTemplateOverlayQO;
import com.turbo.cloud.contract.web.model.overlay.CtContractTemplateOverlayDO;
import com.turbo.cloud.contract.web.model.overlay.CtContractTemplateOverlayVO;
import com.turbo.cloud.contract.web.service.CtContractTemplateOverlayService;
import com.turbo.cloud.core.exception.BusinessException;
import com.turbo.cloud.core.utils.BeanListUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 合同模板覆盖物 逻辑处理类
 * </p>
 *
 * @author zhangluning
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class OverlayLogic {

    final CtContractTemplateOverlayService ctContractTemplateOverlayService;

    /**
     * 查询合同模板表单列表
     *
     * @param ctContractTemplateOverlayQO 查询表单条件
     * @return Page
     */
    public List<CtContractTemplateOverlayVO> queryOverlayListByContractTemplateId(CtContractTemplateOverlayQO ctContractTemplateOverlayQO) {
        /*
         * 模糊查询条件
         */
        LambdaQueryWrapper<CtContractTemplateOverlay> contractTemplateOverlayLQW = new LambdaQueryWrapper<>();

        contractTemplateOverlayLQW.eq(StrUtil.isNotBlank(ctContractTemplateOverlayQO.getContractTemplateId()), CtContractTemplateOverlay::getContractTemplateId, ctContractTemplateOverlayQO.getContractTemplateId());

        contractTemplateOverlayLQW.like(StrUtil.isNotBlank(ctContractTemplateOverlayQO.getOverlayNameByLike()), CtContractTemplateOverlay::getOverlayName, ctContractTemplateOverlayQO.getOverlayNameByLike());
        contractTemplateOverlayLQW.like(StrUtil.isNotBlank(ctContractTemplateOverlayQO.getOverlayCodeByLike()), CtContractTemplateOverlay::getOverlayCode, ctContractTemplateOverlayQO.getOverlayCodeByLike());

        List<CtContractTemplateOverlay> contractTemplateOverlayList = ctContractTemplateOverlayService.list(contractTemplateOverlayLQW);

        List<CtContractTemplateOverlayVO> result = new ArrayList<>();
        BeanListUtils.copyList(contractTemplateOverlayList, result, CtContractTemplateOverlayVO::new);

        return result;
    }

    /**
     * 新增合同模板覆盖物
     *
     * @param ctContractTemplateOverlayDO 实体
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean createContractTemplateOverlay(CtContractTemplateOverlayDO ctContractTemplateOverlayDO) {
        CtContractTemplateOverlay ctContractTemplateOverlay = new CtContractTemplateOverlay();
        BeanUtils.copyProperties(ctContractTemplateOverlayDO, ctContractTemplateOverlay);

        boolean roleSaveResult = ctContractTemplateOverlayService.save(ctContractTemplateOverlay);
        if (!roleSaveResult) {
            throw new BusinessException("新增合同模板覆盖物失败");
        }
        return true;
    }

    /**
     * 修改合同模板覆盖物
     *
     * @param ctContractTemplateOverlayDO 实体
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean modifyContractTemplateOverlay(CtContractTemplateOverlayDO ctContractTemplateOverlayDO) {
        CtContractTemplateOverlay ctContractTemplateOverlay = new CtContractTemplateOverlay();
        BeanUtils.copyProperties(ctContractTemplateOverlayDO, ctContractTemplateOverlay);

        boolean result = ctContractTemplateOverlayService.updateById(ctContractTemplateOverlay);
        if (!result) {
            throw new BusinessException("修改合同模板覆盖物失败");
        }

        return true;
    }
}
